package com.yin.mvc.action;


import com.yin.annotation.YinAutowired;
import com.yin.annotation.YinController;
import com.yin.annotation.YinRequestMapping;
import com.yin.annotation.YinRequestParam;
import com.yin.mvc.service.IDemoService;
import com.yin.mvc.service.IQueryService;
import com.yin.webmvc.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@YinController
@YinRequestMapping("/")
public class DemoAction {

    @YinAutowired
    private IDemoService demoService;


    @YinAutowired
    IQueryService queryService;

    @YinRequestMapping("/f.html")
    public ModelAndView query(@YinRequestParam("teacher") String teacher){
        String result = queryService.query(teacher);
        Map<String,Object> model = new HashMap<>();
        model.put("teacher", teacher);
        model.put("data", result);
        model.put("token", "123456");
        return new ModelAndView("f.html",model);
    }

    @YinRequestMapping("/query")
    public ModelAndView query(HttpServletResponse response,
                              HttpServletRequest request,
                              @YinRequestParam("name") String name){
        String result = demoService.getName(name);
        try {
            response.getWriter().write(result);
            response.getWriter().flush();
            response.getWriter().close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @YinRequestMapping("/")
    public String  add(HttpServletRequest request,
                            HttpServletResponse response)
    {
       return "hello";
    }


}
